//
//  DemoTableViewController.m
//  DemoTableView
//
//  Created by wenchao on 15/12/23.
//  Copyright © 2015年 wenchao. All rights reserved.
//

#import "DemoTableViewController.h"
#import "DemoTableViewCell.h"

@interface DemoTableViewController () {
    NSMutableArray  *_mockData;
}

@end

@implementation DemoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableView.estimatedRowHeight=44.0;
    
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    
    [self.tableView registerClass:[DemoTableViewCell class] forCellReuseIdentifier:[DemoTableViewCell cellIdentifier]];
    
    _mockData = [NSMutableArray array];
    [_mockData addObject:@"hello"];
    [_mockData addObject:@"Uncomment the following line to preserve selection between presentations"];
    [_mockData addObject:@"Uncomment the following line to preserve selection between presentationsUncomment the following line to preserve selection between presentations"];
    [_mockData addObjectsFromArray:@[@"因为，在第一次加载过程中，每一行都会调用tableView:heightForRowAtIndexPath:方法（为了计算滚动条的尺寸）iOS7中，你可以（也绝对应该使用table view的estimatedRowHeight属性。这样会为还不在屏幕范围内的cell提供一个临时估算的行高值。然后，当这些cell即将要滚入屏幕范围内的时候，真实的行高值会被计算出来（通过tableView:heightForRowAtIndexPath:方法），估算的行高就会被替换掉。,",@"一般来说，行高估算值不需要太精确——它只是被用来修正tableView中滚动条的大小的，当你在屏幕上滑动cell的时候，即便估算值不准确，tableView还是能很好地调节滚动条。",@"）一个接近于“平均”行高的常量值即可。只有行高变化很极端的时候（比如相差一个数量级"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_mockData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DemoTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:[DemoTableViewCell cellIdentifier]forIndexPath:indexPath];
    if (_mockData && _mockData.count > 0) {
        cell.textLabel.text = _mockData[indexPath.row];
        
        CGFloat preMaxWaith =[UIScreen mainScreen].bounds.size.width-30;
        [cell.textLabel setPreferredMaxLayoutWidth:preMaxWaith];
        [cell.textLabel layoutIfNeeded];
    }
//    cell.textLabel.text = @"hello";
//    cell.textLabel.backgroundColor = [UIColor redColor];
//    [cell.contentView sendSubviewToBack:cell.textLabel];
    
    return cell;
}


//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static DemoTableViewCell *cell = nil;
//    static dispatch_once_t onceToken;
//    //只会走一次
//    dispatch_once(&onceToken, ^{
//        cell = (DemoTableViewCell*)[tableView dequeueReusableCellWithIdentifier:[DemoTableViewCell cellIdentifier]];
//    });
//    
//    //calculate
//    CGFloat height = [cell calulateHeightWithtTitle:[_mockData objectAtIndex:indexPath.row] desrip:nil];
//    
//    return height;
    
//    NSString  *content = _mockData[indexPath.row];
//    return [DemoTableViewCell calulateHeightWithtTitle:content];
//}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
